package com.mmd.base.utils;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 加密工具类
 * 根据碰撞概率，哈希算法的输出长度越长，就越难产生碰撞，也就越安全。
 * 算法	        输出长度（位）	输出长度（字节）
 * MD5	        128 bits	    16 bytes
 * SHA-1	    160 bits	    20 bytes
 * RipeMD-160	160 bits	    20 bytes
 * SHA-256	    256 bits	    32 bytes
 * SHA-512	    512 bits	    64 bytes
 */
public class EncryptionUtils {
    public static final String MD5 = "MD5";
    public static final String SHA1 = "SHA-1";
    public static final String RipeMD160 = "RipeMD-160";
    public static final String SHA256 = "SHA-256";
    public static final String SHA512 = "SHA-512";


    public static String encryptionMD5(String data) throws NoSuchAlgorithmException {
        return encryption(data,MD5);
    }

    public static String encryptionSHA1(String data) throws NoSuchAlgorithmException {
        return encryption(data,SHA1);
    }

    public static String encryptionRipeMD160(String data) throws NoSuchAlgorithmException {
        return encryption(data,RipeMD160);
    }

    public static String encryptionSHA256(String data) throws NoSuchAlgorithmException {
        return encryption(data,SHA256);
    }

    public static String encryptionSHA512(String data) throws NoSuchAlgorithmException {
        return encryption(data,SHA512);
    }

    /**
     * 加密
     * @param data String
     * @param type
     * {@link EncryptionUtils#MD5}
     * {@link EncryptionUtils#SHA1}
     * {@link EncryptionUtils#RipeMD160}
     * {@link EncryptionUtils#SHA256}
     * {@link EncryptionUtils#SHA512}
     * @return String
     * @throws NoSuchAlgorithmException {@link NoSuchAlgorithmException} 异常
     */
    public static String encryption(String data, String type) throws NoSuchAlgorithmException {
        // 创建一个MessageDigest实例:
        MessageDigest md = MessageDigest.getInstance(type);
        // 反复调用update输入数据:
        md.update(data.getBytes(StandardCharsets.UTF_8));
        byte[] result = md.digest(); // 16 bytes: 68e109f0f40ca72a15e05cc22786f8e6
        String str = new BigInteger(1, result).toString(16);
        System.out.println(str);
        return str;
    }

    //Sha1签名
    public static String getSha1(String str) {
        if (str == null || str.length() == 0) {
            return null;
        }
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f' };

        try {
            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
            mdTemp.update(str.getBytes("UTF-8"));

            byte[] md = mdTemp.digest();
            int j = md.length;
            char buf[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(buf);
        } catch (Exception e) {
            return null;
        }
    }
}
